using Commons;

namespace 计算机窗体
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region 公共运算功能
        private string _inputTxt1 = string.Empty;

        private string _inputTxt2 = string.Empty;

        private string _result = string.Empty;

        private bool _inputFlag = false;

        public OperatorType _operatorType { get; set; } = OperatorType.空;

        public string OperationAdd(double a, double b)
        {
            var result = a + b;
            return result.ToString();
        }

        public string OperationSub(double a, double b)
        {
            var result = a - b;
            return result.ToString();
        }

        public string OperationMul(double a, double b)
        {
            var result = a * b;
            return result.ToString();
        }

        public string OperationDiv(double a, double b)
        {
            if (b == 0)
            {
                MessageBox.Show("被除数不能为0");
                return string.Empty;
            }
            var result = a / b;
            return result.ToString();
        }

        public void OperationClearAll()
        {
            _inputTxt1 = string.Empty;
            _inputTxt2 = string.Empty;
            _result = string.Empty;

            _inputFlag = false;
            lblInput1.Text = "0";
            lblInput2.Text = string.Empty;
        }

        public void OperationClearInput()
        {
            _inputTxt1 = string.Empty;
            lblInput1.Text = "0";
        }

        public void OperationCancel()
        {
            if (!string.IsNullOrEmpty(_inputTxt1) && _inputTxt1.Length > 0)
            {
                _inputTxt1 = _inputTxt1.Substring(0, _inputTxt1.Length - 1);
                lblInput1.Text = _inputTxt1;
            }
        }
        #endregion


        private void btn0_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "0";
            lblInput1.Text = _inputTxt1;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "1";
            lblInput1.Text = _inputTxt1;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "2";
            lblInput1.Text = _inputTxt1;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "3";
            lblInput1.Text = _inputTxt1;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "4";
            lblInput1.Text = _inputTxt1;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "5";
            lblInput1.Text = _inputTxt1;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "6";
            lblInput1.Text = _inputTxt1;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "7";
            lblInput1.Text = _inputTxt1;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "8";
            lblInput1.Text = _inputTxt1;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + "9";
            lblInput1.Text = _inputTxt1;
        }

        private void btnDot_Click(object sender, EventArgs e)
        {
            _inputTxt1 = _inputTxt1 + ".";
            lblInput1.Text = _inputTxt1;
        }

        private void btnSwitch_Click(object sender, EventArgs e)
        {
            if (_inputTxt1.Length > 0)
            {
                if (_inputTxt1.Substring(0, 1) == "-")
                {
                    _inputTxt1 = _inputTxt1.Substring(1, _inputTxt1.Length - 1);
                }
                else
                {
                    _inputTxt1 = "-" + _inputTxt1;
                }
                lblInput1.Text = _inputTxt1;
            }

            //double temp = double.Parse(_inputTxt1);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OperationCancel();
        }

        private void btnClearInput_Click(object sender, EventArgs e)
        {
            OperationClearInput();
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            OperationClearAll();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (_inputFlag) return;
            _operatorType = OperatorType.加;
            lblInput2.Text = _inputTxt1 + " + ";
            _inputTxt2 = _inputTxt1;

            _inputFlag = true;
            _inputTxt1 = string.Empty;
            lblInput1.Text = string.Empty;
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            if (_inputFlag) return;
            _operatorType = OperatorType.减;
            lblInput2.Text = _inputTxt1 + " - ";
            _inputTxt2 = _inputTxt1;

            _inputFlag = true;
            _inputTxt1 = string.Empty;
            lblInput1.Text = string.Empty;
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            if (_inputFlag) return;
            _operatorType = OperatorType.乘;
            lblInput2.Text = _inputTxt1 + " x ";
            _inputTxt2 = _inputTxt1;

            _inputFlag = true;
            _inputTxt1 = string.Empty;
            lblInput1.Text = string.Empty;
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            if (_inputFlag) return;
            _operatorType = OperatorType.除;
            lblInput2.Text = _inputTxt1 + " / ";
            _inputTxt2 = _inputTxt1;

            _inputFlag = true;
            _inputTxt1 = string.Empty;
            lblInput1.Text = string.Empty;
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            double a = double.Parse(_inputTxt1);
            double b = double.Parse(_inputTxt2);
            var res = SwitchHelper.SwitchMethod(a, b, _operatorType);
            lblInput2.Text += $"{_inputTxt1} = {res}";
            lblInput1.Text = string.Empty;
            _inputFlag = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
